package core;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import protocol.Request;
import protocol.Response;
import protocol.changePassword.ChangePasswordRequest;
import protocol.changePassword.ChangePasswordResponse;
import protocol.login.LoginRequest;
import protocol.login.LoginResponse;
import protocol.vendasFuncionario.VendasFuncionarioRequest;
import protocol.vendasFuncionario.VendasFuncionarioResponse;
import protocol.vendasProduto.VendasProdutoRequest;
import protocol.vendasProduto.VendasProdutoResponse;

public class Connection extends Thread {
	public static final String SERVER_ADDR = "192.168.0.161"; 
	public static final String SERVER_PORT = "80";
	
	private boolean running = true;
	
	private Request request;
	private Response response;
	
	public Connection() {}

	public void run() {
		while (running) {
			if (!running) {
				break;
			}
			synchronized (this) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
				// realiza conexão...
			if (request instanceof LoginRequest) {
				String url = "http://" + SERVER_ADDR + ":" + SERVER_PORT + request.getRequest();
				this.response = new LoginResponse(doConnect(url));
			} else if (request instanceof VendasFuncionarioRequest) {
				String url = "http://" + SERVER_ADDR + ":" + SERVER_PORT + request.getRequest();
				System.out.println(url);
				this.response = new VendasFuncionarioResponse(doConnect(url));
			} else if (request instanceof VendasProdutoRequest) {
				String url = "http://" + SERVER_ADDR + ":" + SERVER_PORT + request.getRequest();
				System.out.println(url);
				this.response = new VendasProdutoResponse(doConnect(url));
			} else if (request instanceof ChangePasswordRequest) {
				String url = "http://" + SERVER_ADDR + ":" + SERVER_PORT + request.getRequest();
				System.out.println(url);
				this.response = new ChangePasswordResponse(doConnect(url));
			}
			synchronized (this) {
				this.notifyAll();
			}
			
		}
	}

	public Request getRequest() {
		return this.request;
	}

	public void setRequest(Request request) {
		this.request = request;
	}

	public Response getResponse() {
		return this.response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}
	
	public String doConnect(String url) {
		HttpConnection http = null;
		ByteArrayOutputStream baos = null;
		DataInputStream dis = null;
		try {
			http = (HttpConnection) Connector.open(url);
		http.setRequestMethod(HttpConnection.GET);
		
		baos = new ByteArrayOutputStream();
//		if (http.getResponseCode() == 200) {
		dis = http.openDataInputStream();
			
		while (dis.available() > 0) {
			baos.write(dis.read());
		}
//		} else {
//			// falha gen�rica
//		}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (dis != null) {
					dis.close();
				}
				if (http != null) {
					http.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return new String(baos.toByteArray());
	}
}
